/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.amse.grigory.dchess.io;
import java.io.*;
import org.amse.grigory.dchess.kernel.*;

/**
 *
 * @author grigory
 */
public class GameSaver {
    public static void save(IModel model, File destination) throws FileNotFoundException, IOException{
        //FileOutputStream out= new FileOutputStream(destination);
        //BufferedOutputStream stream=new BufferedOutputStream(out);
        FileWriter out = new FileWriter(destination);
        out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        out.write("<game>\n");
        out.write("<rules name=\""+model.getRulesFile()+"\">\n");
        out.write("</rules>\n");
        int t1=model.getBlackTime();
        int t2=model.getWhiteTime();
        out.write("<time black=\""+t1+"\" white=\""+t2+"\">\n");
        out.write("</time>\n");
        out.write("<moves>\n");
        int n = model.getMovesCount();
        for (int i=0; i<n; i++){
            String s="sx=\"";
            s+=model.getMoveCoordinates(i).getSX();
            s+="\" sy=\"";
            s+=model.getMoveCoordinates(i).getSY();
            s+="\" ex=\"";
            s+=model.getMoveCoordinates(i).getEX();
            s+="\" ey=\"";
            s+=model.getMoveCoordinates(i).getEY();
            s+="\" >";
            out.write(" <move "+s+"\n");
            out.write(" </move>\n");
        }
        out.write("</moves>\n");
        out.write("</game>\n");
        out.close();    

    }
}
